package com.example.administrator.fragmentdome3;

import android.animation.Animator;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class ContentFragment extends Fragment {

    /**
     * 需要传入的参数的名称
     */
    public static final String INTENT_TEXT = "text";
    public static final String RESPONSE = "response";
    public static final int REQUEST_LOGIN = 0x112;
    public static final int RESULT_LOGIN = 0x113;

    private String title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            title = getArguments().getString(INTENT_TEXT);
            Intent intent = new Intent();
            intent.putExtra(RESPONSE, "good");
            getActivity().setResult(ListTitleFragment.RESULT_DETAIL, intent);
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Random random = new Random();
        TextView tv = new TextView(getActivity());
        tv.setText(title);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundColor(Color.argb(random.nextInt(100), random.nextInt(255),
                random.nextInt(255),random.nextInt(255)));
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginDialogFragment loginDialogFragment = new LoginDialogFragment();
                loginDialogFragment.setTargetFragment(ContentFragment.this, REQUEST_LOGIN);
                loginDialogFragment.show(getFragmentManager(), "login");
            }
        });
        return tv;
    }

    /**
     * 需要传入的参数
     * @param argument
     * @return
     */
    public static ContentFragment newInstance(String argument) {
        ContentFragment contentFragment = new ContentFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_TEXT, argument);
        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_LOGIN && resultCode == RESULT_LOGIN && data != null) {
            String name = data.getStringExtra("name");
            String pwd = data.getStringExtra("pwd");
            Toast.makeText(getActivity(), "name:" + name + ",\npwd:" + pwd, Toast.LENGTH_LONG).show();
        }
    }
}
